/*
 * @Description:
 * @Version: 1.0
 * @Autor: zhangwei
 * @Date: 2023-08-18 14:14:11
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-06-24 17:00:08
 */

import {
  addKnowledgeFile,
  analysisFile,
  deleteKnowledgeFile,
  getFileJson,
} from '@/services/ant-design-pro/corpus/index';
import { getKnowledgeList } from '@/services/ant-design-pro/home';
import type { ActionType } from '@ant-design/pro-components';
import { useLocation, useModel } from '@umijs/max';
import { useRequest } from 'ahooks';
import { Form, message } from 'antd';
import { useRef, useState } from 'react';
import { createContainer } from 'unstated-next';
export default createContainer(() => {
  const [editFile, setEditFile] = useState(false);

  const [contentVisible, setContentVisible] = useState(false);

  const actionRef = useRef<ActionType>();

  const [data, setData] = useState<any>([]);

  const [content, setContent] = useState('');

  const [formCorpus] = Form.useForm();

  const location: Record<string, any> = useLocation();

  const id = location?.state?.knowledgeId ? location?.state?.knowledgeId : '';

  const knowledgeType = location?.state?.type ? location?.state?.type : '';

  const [knowledgeId, setKnowledgeId] = useState<any>(id);

  const [type, setType] = useState<any>(knowledgeType);

  const [form] = Form.useForm();

  const { initialState } = useModel('@@initialState');
  const { currentUser } = initialState || {};
  const getData = useRequest(
    async (val: any) => {
      const res = await getKnowledgeList({
        knowledgeId: knowledgeId,
        type: type,
        ...val,
        deptCode: currentUser?.deptCode,
      });
      setData(res?.data);
    },
    { manual: true },
  );

  const addKnowledge = useRequest(
    async (vals: object) => {
      const res: any = await addKnowledgeFile({ ...vals, knowledgeId: knowledgeId });
      if (res) {
        message.success('新增成功!', 0.5);

        setEditFile(false);
        form.resetFields();

        actionRef.current?.reload();
      } else {
        message.error(`新增失败，错误信息：!`, 0.5);
      }
    },
    { manual: true },
  );

  const getAnalysisFile = useRequest(
    async (val) => {
      const res = await analysisFile({ fileId: val });
      if (res) {
        message.success('解析成功!', 0.5);

        actionRef.current?.reload();
      } else {
        message.error(`解析失败，错误信息：!`, 0.5);
      }
    },
    { manual: true },
  );
  const getKnowledgeJson = useRequest(
    async (vals: object) => {
      const res: any = await getFileJson({ ...vals });
      if (res) {
        message.success('拆条成功!', 0.5);
        setContent(res.data);

        setContentVisible(true);
      } else {
        message.error(`拆条失败，错误信息：!`, 0.5);
      }
    },
    { manual: true },
  );

  const deleteFile = useRequest(
    async (val) => {
      const res = await deleteKnowledgeFile({ fileId: val });
      if (res) {
        message.success('删除成功!', 0.5);
        actionRef.current?.reload();
      } else {
        message.error(`删除失败，错误信息：!`, 0.5);
      }
    },
    { manual: true },
  );

  return {
    form,
    editFile,
    setEditFile,
    contentVisible,
    setContentVisible,
    addKnowledgeFile,
    addKnowledge,
    getData,
    data,
    setData,
    knowledgeId,
    setKnowledgeId,
    actionRef,
    deleteFile,
    getKnowledgeJson,
    formCorpus,
    content,
    setContent,

    type,
    setType,
    getAnalysisFile,
  };
});
